package admin

// by daoyi
import (
	"log"
	"simai_oa/controller"
	"simai_oa/model"

	"github.com/gin-gonic/gin"
)

type Role struct{}

// List
//
//	@Summary	分页数据
//	@Tags		后台-角色管理
//	@param		page		query		int		false	"多少页"
//	@param		limit		query		int		false	"每页多少条"
//	@param		sort		query		string	false	"排序"
//	@param		key			query		string	false	"搜索条件"
//	@param		parent_id	query		string	false	"父Id"
//	@Success	200			{string}	json{"code","message","data"}
//	@Router		/admin/role/list [GET]
func (Role) List(c *gin.Context) {
	page := controller.GetPageIndex(c)
	limit := controller.GetPageLimit(c)
	sort := controller.GetPageSort(c)
	key := controller.GetPageKey(c)
	parent_id := controller.GetQueryToUint64(c, "parent_id")
	var whereOrder []model.PageWhereOrder
	order := "ID DESC"
	if len(sort) >= 2 {
		orderType := sort[0:1]
		order = sort[1:len(sort)]
		if orderType == "+" {
			order += " ASC"
		} else {
			order += " DESC"
		}
	}
	whereOrder = append(whereOrder, model.PageWhereOrder{Order: order})
	if key != "" {
		v := "%" + key + "%"
		var arr []interface{}
		arr = append(arr, v)
		whereOrder = append(whereOrder, model.PageWhereOrder{Where: "name like ?", Value: arr})
	}
	if parent_id > 0 {
		var arr []interface{}
		arr = append(arr, parent_id)
		whereOrder = append(whereOrder, model.PageWhereOrder{Where: "parent_id = ?", Value: arr})
	}
	var total int64
	list := []model.Role{}
	err := model.GetPage(&model.Role{}, &model.Role{}, &list, page, limit, &total, whereOrder...)
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	controller.ResSuccessPage(c, total, &list)
}

// Detail
//
//	@Summary	详情
//	@Tags		后台-角色管理
//	@param		id	query		int	false	"角色id"
//	@Success	200	{string}	json{"code","message","data"}
//	@Router		/admin/role/detail [GET]
func (Role) Detail(c *gin.Context) {
	id := controller.GetQueryToUint64(c, "id")
	var models model.Role
	where := model.Role{}
	where.ID = id
	_, err := model.First(&where, &models)
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	controller.ResSuccess(c, &models)
}

// Update
//
//	@Summary	更新
//	@Tags		后台-角色管理
//	@param		id			query		int	false	"id"
//	@param		memo		query		int	false	"备注"
//	@param		name		query		int	false	"名称"
//	@param		sequence	query		int	false	"排序值"
//	@param		parent_id	query		int	false	"父id"
//	@Success	200			{string}	json{"code","message"}
//	@Router		/admin/role/update [POST]
func (Role) Update(c *gin.Context) {
	models := model.Role{}
	err := c.Bind(&models)
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	err = model.Save(&models)
	if err != nil {
		log.Println(err)
		controller.ResFail(c, "操作失败")
		return
	}
	controller.ResSuccessMsg(c)
}

// Create
//
//	@Summary	新增
//	@Tags		后台-角色管理
//	@param		memo		query		int	false	"备注"
//	@param		name		query		int	false	"名称"
//	@param		sequence	query		int	false	"排序值"
//	@param		parent_id	query		int	false	"父id"
//	@Success	200			{string}	json{"code","message"}
//	@Router		/admin/role/create [POST]
func (Role) Create(c *gin.Context) {
	models := model.Role{}
	err := c.Bind(&models)
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	err = model.Create(&models)
	if err != nil {
		log.Println(err)
		controller.ResFail(c, "操作失败")
		return
	}
	controller.ResSuccess(c, gin.H{"id": models.ID})
}

// Delete
//
//	@Summary	删除数据
//	@Tags		后台-角色管理
//	@param		ids	query		int	false	"ids"
//	@Success	200	{string}	json{"code","message"}
//	@Router		/admin/role/delete [POST]
func (Role) Delete(c *gin.Context) {
	var ids []uint64
	err := c.Bind(&ids)
	if err != nil || len(ids) == 0 {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	role := model.Role{}
	err = role.Delete(ids)
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	go model.CsbinDeleteRole(ids)
	controller.ResSuccessMsg(c)
}

// AllRole
//
//	@Summary	所有角色
//	@Tags		后台-角色管理
//	@Success	200	{string}	json{"code","message","data"}
//	@Router		/admin/role/allrole [GET]
func (Role) AllRole(c *gin.Context) {
	var list []model.Role
	err := model.Find(&model.Role{}, &list, "parent_id asc", "sequence asc")
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	controller.ResSuccess(c, &list)
}

// RoleMenuIDList
//
//	@Summary	获取角色下的菜单ID列表
//	@Tags		后台-角色管理
//	@param		roleid	query		int	false	"角色id"
//	@Success	200		{string}	json{"code","message","data"}
//	@Router		/admin/role/rolemenuidlist [GET]
func (Role) RoleMenuIDList(c *gin.Context) {
	roleid := controller.GetQueryToUint64(c, "roleid")
	menuIDList := []uint64{}
	where := model.RoleMenu{RoleID: roleid}
	err := model.PluckList(&model.RoleMenu{}, &where, &menuIDList, "menu_id")
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	controller.ResSuccess(c, &menuIDList)
}

// SetRole
//
//	@Summary	设置角色菜单权限
//	@Tags		后台-角色管理
//	@param		roleid	query		int		false	"角色id"
//	@param		menuids	query		string	false	"data中放菜单ids"
//	@Success	200		{string}	json{"code","message"}
//	@Router		/admin/role/setrole [POST]
func (Role) SetRole(c *gin.Context) {
	roleid := controller.GetQueryToUint64(c, "roleid")
	var menuids []uint64
	err := c.Bind(&menuids)

	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}

	rm := model.RoleMenu{}
	err = rm.SetRole(roleid, menuids)
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}
	go model.CsbinSetRolePermission(roleid)
	controller.ResSuccessMsg(c)
}
